package com.swjd.servlet;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.swjd.bean.user;

public class usersvlt extends HttpServlet {

	public usersvlt() {
		super();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	boolean flag;
	public void doGet(HttpServletRequest req, HttpServletResponse res)
	    throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8"); 	
	    String userid =req.getParameter("userid");
	    int success = 0;
	    String action = req.getParameter("action");
	    user u = null;
	    String message="";

	   /* if ("update".equalsIgnoreCase(action)) {
	    	flag=true;	
	     	u = doUpdate(req,res, userid);
	     	if(flag)sendBean(req,res,u,"/userinfo.jsp");
	     	    
	    }*/
				   
	    if ("delete".equalsIgnoreCase(action)) {
	    	try{			
	      	success = doDelete(userid);	
	      	    }
	      	    catch(SQLException e){}				
	    	if (success != 1) {
	    		doError(req, res, "usersvlt: Delete unsuccessful. Rows affected: " + success);
	    	} else {
	    		res.sendRedirect("userinfo.jsp");
	    	}
	   
	    }
	    }
	    

	/* public user doNew(HttpServletRequest req,HttpServletResponse res )
	                           throws ServletException,IOException{
	      user stu= new u();                     	
	      String userid=req.getParameter("useid");
	      String name=req.getParameter("name");
	      String password= req.getParameter("password");
	      String dep=req.getParameter("dep");
	      String sex = req.getParameter("sex");
	      String jiguan = req.getParameter("jiguan");
	      if(isTrue(req,res,stu_id1,name,password)&& hasLogin(req,res,stu_id1)){
	      
	      stu.setId(stu_id1);
	      stu.setName(name);
	      stu.setPassword(password);
	      stu.setDep(dep);
	      stu.setSex(sex);
	      stu.setJiguan(jiguan);
	      stu.addStudent(); }  
	      return stu;                	
	                           	
	                           	}

	 public user doUpdate(HttpServletRequest req,HttpServletResponse res ,String userid)
	                           throws ServletException,IOException {                      	
	    user u = new user();   
	    String username=new String(req.getParameter("username").getBytes("gbk"));  	                 
	    String email = req.getParameter("email");
	    String id=req.getParameter("id");
	    String sex = req.getParameter("sex");
	    String phone=req.getParameter("phone");
	    String address=req.getParameter("address");
	    if(isTrue(req,res,username,email,id,phone,address)){
	    u.setuserId(userid);
	    u.setuserName(username);
	    u.setEmail(email);
	    u.setAddress(address);
	    u.setPhone(phone);
	    u.setId(id);
	    u.setSex(sex);
	    u.updateUser();       }
			return u;
	  }*/

	  public int doDelete(String userid) throws SQLException {
	  	int num=0;
	    user u=new user();
	    num=u.deleteUser(userid);
	    return num;
	  }
		
	public void sendBean(HttpServletRequest req, HttpServletResponse res,
	                       user uu, String target)
	                       throws ServletException, IOException {
	    req.setAttribute("user", uu);
	    RequestDispatcher rd = getServletContext().getRequestDispatcher(target);
	    rd.forward(req, res);
	  }
	  
	  
	  public void doError(HttpServletRequest req,
	                      HttpServletResponse res,
	                      String str)
	                      throws ServletException, IOException {

		 flag=false;
	    req.setAttribute("problem", str);
	    RequestDispatcher rd = getServletContext().getRequestDispatcher("/errorpage.jsp");
	    rd.forward(req, res);
	  }
	  
	  public boolean hasLogin(HttpServletRequest req, HttpServletResponse res,String userid)
	  throws ServletException, IOException{
	  	boolean f=true;
	  	String message="对不起，该用户已经被注册过了!";
	  	user u= new user();
	  	f= u.hasLogin(userid);
	  	if(f==false){
	  	doError(req,res,message);	
	  		}
	  		return f;
	  	}
	  
	  public boolean isTrue(HttpServletRequest req, HttpServletResponse res,
	                        String name,String email,String id,String phone,String address)
	                        throws ServletException, IOException {
	   boolean f=true;                     	
	   String message ="";
	   
	   if(name==null || name.equals(""))  {
	   	f=false;
	   	message="姓名不能为空，请重新填写！";
	   	if(flag)doError(req,res,message);	}
	  	
	       
	   if(email==null || email.equals(""))  {
	   	f=false;
	   	message="电子邮箱不能为空，请重新填写！";
	   	if(flag) doError(req,res,message);	}  
       
	   if(id==null || id.equals(""))  {
		   	f=false;
		   	message="身份证号不能为空，请重新填写！";
		   	if(flag) doError(req,res,message);	} 
	   if(phone==null || phone.equals(""))  {
		   	f=false;
		   	message="电话不能为空，请重新填写！";
		   	if(flag) doError(req,res,message);	} 
	   if(address==null || address.equals(""))  {
		   	f=false;
		   	message="联系地址不能为空，请重新填写！";
		   	if(flag) doError(req,res,message);	} 
	     return f;
	     
	  }
	  
	  public void doPost(HttpServletRequest req, HttpServletResponse res)
	    throws ServletException, IOException {

	    doGet(req, res);

	  }

}
